﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public partial class UserControls_EBookList : System.Web.UI.UserControl
{
    private ITemplate _ItemTemplate;
    private ITemplate _AlternateItemTemplate;
    private ITemplate _HeaderTemplate;
    private ITemplate _FooterTemplate;
    private IEnumerable _DataSource;

    public int ColumnCount
    {
        get;
        set;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        
    }

    [TemplateContainer(typeof(SimpleItemTemplate))]
    public ITemplate ItemTemplate
    {
        get { return _ItemTemplate; }
        set { _ItemTemplate = value; }
    }

    [TemplateContainer(typeof(SimpleItemTemplate))]
    public ITemplate AlternateItemTemplate
    {
        get { return _AlternateItemTemplate; }
        set { _AlternateItemTemplate = value; }
    }

    [TemplateContainer(typeof(SimpleItemTemplate))]
    public ITemplate HeaderTemplate
    {
        get { return _HeaderTemplate; }
        set { _HeaderTemplate = value; }
    }

    [TemplateContainer(typeof(SimpleItemTemplate))]
    public ITemplate FooterTemplate
    {
        get { return _FooterTemplate; }
        set { _FooterTemplate = value; }
    }

    public IEnumerable DataSource
    {
        get { return _DataSource; }
        set { _DataSource = value; }
    }

    public override void DataBind()
    {
        //Rendering Header template into current control
        if (HeaderTemplate != null)
        {
            AddTemplateAsControl(HeaderTemplate, null);
        }

        IEnumerator ie = _DataSource.GetEnumerator();
        bool renderAlternateTemplate = false;

        while (ie.MoveNext())
        {
            if (renderAlternateTemplate && AlternateItemTemplate != null)
            {
                AddTemplateAsControl(ItemTemplate, ie.Current);
            }
            else if (AlternateItemTemplate != null)
            {
                AddTemplateAsControl(AlternateItemTemplate, ie.Current);
            }
            else
            {
                //don't render anything
            }

            renderAlternateTemplate = !renderAlternateTemplate;
        }

        //Rendering footer template into current control
        if (FooterTemplate != null)
            AddTemplateAsControl(FooterTemplate, null);

        //Always better to call base class implementation
        base.DataBind();
    }

    private void AddTemplateAsControl(ITemplate anyTemplate, object cuurentItem)
    {
        SimpleItemTemplate templateContentHolder = new SimpleItemTemplate(cuurentItem);
        anyTemplate.InstantiateIn(templateContentHolder);
        myEBookList.Controls.Add(templateContentHolder);
    }

    protected override void Render(HtmlTextWriter writer)
    {
        base.Render(writer);
        if (ColumnCount == 0)
            ColumnCount = 1;

        string strScript = string.Format(@"<script language='javascript' type='text/javascript'>
                            var ColumnCount = {0};
                            </script>", ColumnCount);

        if (!Page.ClientScript.IsStartupScriptRegistered("ColCount"))
        {
            Page.ClientScript.RegisterStartupScript(typeof(Page), "ColCount", strScript);
        }
    } 
}